package edu.hsbremen.android;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;


public class addContact extends ListActivity {
	private Cursor contactList;
	private SimpleCursorAdapter contactListAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selectcontact);

		load();
	}

	private void load() {
		contactList = getContentResolver().query(Contacts.CONTENT_URI, new String[] {PhoneLookup._ID, PhoneLookup.DISPLAY_NAME}, null, null, null);	
		contactListAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, contactList,	new String[] {PhoneLookup.DISPLAY_NAME},new int[] {android.R.id.text1});
		setListAdapter(contactListAdapter);
	}

	@Override
	protected void onListItemClick(ListView liste, View view, int position, long id) {
		TextView textView = (TextView) view;
		String contact = (String) textView.getText();
		sendContact(contact);
	}
	
	private void sendContact(String data){
		Intent intent = new Intent();
		intent.putExtra("contact", data);
		setResult(0, intent);
		finish();
	}
}
